package com.taxshare.operation.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.operation.entity.CodeDetailEntity;
import com.taxshare.operation.mapper.CodeDetailMapper;
import com.taxshare.operation.view.vo.CodeDetailVO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;

/**
 * @ClassName: CommonCodeService
 * @Description: (通用码表明细)
 * @author liupp
 * @date 2021年1月22日
 *
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class CodeDetailService extends ServiceImpl<CodeDetailMapper, CodeDetailEntity> {

	/**
	 * @Title: getDetailInfoList
	 * @Description: (通码明细分页)
	 * @param @param codeDetailVO
	 * @param @return 参数
	 * @return Response<List<CodeDetailVO>> 返回类型
	 * @throws
	 */
	public Response queryPageList(PageRequest<CodeDetailVO> query) {
		QueryWrapper<CodeDetailEntity> queryWrapper = new QueryWrapper<>();
		String name = query.getInfo().getName();
		if (StringUtils.isNotEmpty(name)) {
			queryWrapper.like("t.name", name);
		}
		String code = query.getInfo().getCode();
		if (StringUtils.isNotEmpty(code)) {
			queryWrapper.like("t.code", code);
		}
		String currentCollectionCode = query.getInfo().getCurrentCollectionCode();
		if (StringUtils.isNotEmpty(currentCollectionCode)) {
			queryWrapper.like("t.current_collection_code", currentCollectionCode);
		}
		String parentCode = query.getInfo().getParentCode();
		if (StringUtils.isNotEmpty(parentCode)) {
			queryWrapper.like("t.parent_code", parentCode);
		}
		queryWrapper.orderByDesc("t.create_time");
        IPage<CodeDetailEntity> page = new Page<>(query.getPage().getCurrentPage(),query.getPage().getPageSize());
        IPage<CodeDetailEntity> companyEntities = baseMapper.pageCodeDetail(page,queryWrapper);
        return Response.success(companyEntities);

	}
	
	
	

	/**
	 * @Title: queryList
	 * @Description: (查找通用码表明细列表)
	 * @param @param commonCodeInfoDTO
	 * @param @return 参数
	 * @return Response<List<CommonCodeInfoVO>> 返回类型
	 * @throws
	 */
	public Response<List<CodeDetailVO>> queryDetailList(String currentCollectionCode) {
		QueryWrapper<CodeDetailEntity> queryWrapper = new QueryWrapper<>();
		if (StringUtils.isNotEmpty(currentCollectionCode)) {
			queryWrapper.like("current_collection_code", currentCollectionCode);
		}
		List<CodeDetailEntity> list = this.list(queryWrapper);
		return Response.success(DozerBeanUtil.mapList(list, CodeDetailVO.class));
	}

	/**
	 * 明细,新增
	 */
	public Response addDetailInfo(CodeDetailVO pojo) {
		CodeDetailEntity infoBean = DozerBeanUtil.map(pojo, CodeDetailEntity.class);
		infoBean.setUuid(UUIDUitl.UUID32(false));
		infoBean.setCode(pojo.getCode().trim());
		infoBean.setName(pojo.getName());
		infoBean.setExpandFieldOne(pojo.getExpandFieldOne());
		infoBean.setParentCode(pojo.getParentCode());
		infoBean.setSort(pojo.getSort());
		infoBean.setExpire(pojo.getExpire());
		infoBean.setCreateTime(new Date());
		infoBean.setUpdateTime(new Date());
		this.save(infoBean);
		return Response.success();
	}

	/**
	 * 明细,编辑
	 */
	public Response updateDetailInfo(CodeDetailVO pojo) {
		CodeDetailEntity infoBean = DozerBeanUtil.map(pojo, CodeDetailEntity.class);
		infoBean.setUpdateTime(new Date());
		return Response.success(this.updateById(infoBean));
	}

	/**
	 * 明细,修改状态
	 */
	public Response updateExpireStatus(CodeDetailVO pojo) {
		CodeDetailEntity updateinfo = this.getById(pojo.getUuid());
		updateinfo.setExpire(pojo.getExpire());
		updateinfo.setUpdateTime(new Date());
		return Response.success(this.updateById(updateinfo));
	}

	/**
	 * 明细,删除
	 */
	public Response deleteDetailInfo(String uuid) {
		return Response.success(this.removeById(uuid));
	}
}
